home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 21
/
Cream of the Crop 21 (Terry Blount) (October 1996).iso
/
program
/
libkb100.zip
/
LIBKB-1.00
/
SAMPLES
/
TUBE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-23
|
12KB
|
572 lines
/* tube.c -- test program for libkb with graphic and sound libraries
* Copyright (C) Markus F.X.J. Oberhumer and James Johnson
* For conditions of distribution and use, see copyright notice in kb.h
*/
/* note: There is not much keyboard action in this program.
* This is mainly a demonstration of the safety features of
* libkb when allocating hardware resources.
*/
/* note: I've made many changes to this program, original info follows */
/*************************************************************************
Program : Tubular
File : tube.cpp
Programmer : James Johnson <plexus@stein.u.washington.edu>
Date : 1-14-94
Version : 1.0
************************************************************************/
#if defined(__EMX__)
# include <sys/emx.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <assert.h>
#include <string.h>
#include <kb.h>
#if defined(__KB_MSDOS)
# include "_kb.h" /* KB_INT86, KB_INP8, KB_OUTP8 */
#endif
#include "intro.h"
#if defined(__KB_LINUX)
# define stricmp strcasecmp /* why is this missing ??? */
#endif
#if !defined(__inline__) && !defined(__GNUC__)
# if defined(__cplusplus)
# define __inline__ inline
# else
# define __inline__ /* nothing */
# endif
#endif
static int in_graphics = 0;
static int in_sound = 0;
static int is_win = -1;
#include "tube.h"
/***********************************************************************
// Standalone BogoMips program v1.3 by Jeff Tranter
//
// Based on code Linux kernel code in init/main.c and
// include/linux/delay.h
//
// For more information on interpreting the results,
// see the BogoMIPS Mini-HOWTO document.
************************************************************************/
/* portable version */
static void bogo_delay(long loops)
{
long i;
for (i = loops; i >= 0 ; i--)
;
}
/* return 33.55 BogoMips as 3355 */
long bogomips(long min_interval_msec)
{
clock_t ticks, min_ticks;
unsigned long loops_per_sec = 1;
if (min_interval_msec < 10) /* at least 10 millisecs */
min_interval_msec = 10;
min_ticks = (min_interval_msec * (long)(CLOCKS_PER_SEC)) / 1000;
if (min_ticks < 2)
min_ticks = 2;
while ((loops_per_sec <<= 1) != 0)
{
ticks = clock();
bogo_delay(loops_per_sec);
ticks = clock() - ticks;
if (ticks >= min_ticks)
return ((loops_per_sec / ticks) * (long)(CLOCKS_PER_SEC)) / 5000;
}
return -1;
}
long print_bogomips(FILE *f, long min_interval_msec)
{
long bm;
fprintf(f,"Calibrating delay loop.. ");
fflush(f);
bm = bogomips(min_interval_msec);
if (bm <= 0)
fprintf(f,"failed\n");
else
fprintf(f,"ok - %lu.%02lu BogoMips\n", bm / 100, bm % 100);
fflush(f);
return bm;
}
/***********************************************************************
//
************************************************************************/
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define Min_Z (1 << 3) /* color 1 */
#define Max_Z ((64 << 3) - 1) /* color 63 */
#define Min_Dz 2
#define Max_Dz 64
#define Def_DZ 3
#define Step 20
#define Max_Rand 256
#define Max_Active ((Max_Z / 2) * Step)
typedef struct
{
int x;
int y;
int z;
int Old_x;
int Old_y;
int xc;
int yc;
}
Star;
#if defined(__KB_MSDOS16)
typedef Star huge *PStar;
static huge Star star[Max_Active];
#else
typedef Star *PStar;
static Star star[Max_Active];
#endif
static int Xtable[Max_Rand];
static int Ytable[Max_Rand];
static int Index[Max_Rand];
/* setup colors - lowest z value is nearest star (should be brightest) */
/* play around - this really changes the way it looks */
/* colors 64-255 are not used, so you can still add plenty of stuff */
void initpal(void)
{
int i;
setcolor(0,0,0,15);
for (i = 1; i < 64; i++)
{
#if 1
int tmp = (int) (pow(64.0,(48-i)/48.0) + 0.5);
if (tmp > 63)
tmp = 63;
else if (tmp < 1)
tmp = 1;
#else
unsigned tmp = (unsigned) (pow(64,i/48.0) + 0.5);
tmp = (tmp > 63) ? 63 : tmp;
tmp = 63 - tmp;
#endif
setcolor(i, tmp, tmp, (tmp < 15) ? 15 : tmp);
}
}
/***********************************************************************
// init and draw the tube
************************************************************************/
static void init(void)
{
int i, k, tmp;
unsigned Randnum;
for (i = 0; i < Max_Active; i++)
star[i].z = 0;
for (i = 0; i < Max_Rand; i++)
{
Xtable[i] = (int)(128 * cos(i * 2*M_PI / Max_Rand) + 0.5);
Ytable[i] = (int)(128 * sin(i * 2*M_PI / Max_Rand) + 0.5);
Index[i] = i;
}
/* shuffle Index[] */
for (k = 0; k < Max_Rand/2; k++)
for (i = 0; i < Max_Rand; i++)
{
Randnum = rand() % Max_Rand;
tmp = Index[Randnum];
Index[Randnum] = Index[i];
Index[i] = tmp;
}
}
static unsigned long doit(void)
{
int i, k;
int dz = Def_DZ;
unsigned Tube_Xr = 60;
unsigned Tube_Yr = 60;
unsigned X_choice = 0;
unsigned Y_choice = 0;
unsigned X_count = 64;
unsigned Y_count = 0;
unsigned D_tube_xr = 64;
unsigned D_tube_yr = 0;
int Num_Active = 0;
int First_Free = 0;
unsigned count = 0;
unsigned long frames = 0;
for (;;)
{
#if defined(USE_MIKMOD)
if (in_sound && mod)
{
MD_Update();
if (MP_Ready())
MikMod_PlayMod(mod); /* restart the MOD */
}
#endif
/* fill in new stars */
i = First_Free;
for (k = 0; k < Step && Num_Active < Max_Active; k++)
{
while (star[i].z != 0)
i++;
star[i].x = Tube_Xr * Xtable[Index[X_choice]];
star[i].y = Tube_Yr * Ytable[Index[Y_choice]];
star[i].z = Max_Z;
star[i].xc = WIDTH/2 + (120 * Xtable[X_count]) / Max_Rand;
star[i].yc = HEIGHT/2 + ( 80 * Ytable[Y_count]) / Max_Rand;
if (++X_choice >= Max_Rand)
X_choice = 0;
if (++Y_choice >= Max_Rand)
Y_choice = 0;
Num_Active++;
i++;
}
/* move stars */
#if defined(__DJGPP__) && !defined(USE_ALLEGRO)
_farsetsel(_dos_ds); /* set selector for VIDMEM */
#endif
for (i = 0; i < Max_Active; i++)
{
PStar s = &star[i];
if (s->z == 0) /* star is not active */
continue;
if (s->z > Min_Z)
{
/* calculate the new one */
int X_tmp = s->x / s->z + s->xc;
/* if x lies within boundaries */
if (X_tmp >= 0 && X_tmp < WIDTH)
{
int Y_tmp = s->y / s->z + s->yc;
/* if y lies within boundaries.*/
if (Y_tmp >= 0 && Y_tmp < HEIGHT)
{
if (X_tmp != s->Old_x || Y_tmp != s->Old_y)
{
/* erase the old star */
setpixel(s->Old_x,s->Old_y,0);
/* draw new star */
s->Old_x = X_tmp;
s->Old_y = Y_tmp;
setpixel(X_tmp,Y_tmp,s->z >> 3);
}
s->z -= dz;
continue; /* <- done */
}
}
}
/* erase the old star */
setpixel(s->Old_x,s->Old_y,0);
/* remove the star */
s->z = 0;
Num_Active--;
if (i < First_Free)
First_Free = i;
}
X_count += 2;
if (X_count >= Max_Rand) X_count = 0;
Y_count++;
if (Y_count >= Max_Rand) Y_count = 0;
/* this section increases the X and Y radii, giving ellipses */
if (++count >= 5)
{
count = 0;
Tube_Xr = 20 * Xtable[D_tube_xr] / Max_Rand + 60;
Tube_Yr = 20 * Ytable[D_tube_yr] / Max_Rand + 60;
if(++D_tube_xr >= Max_Rand) D_tube_xr = 0;
if(++D_tube_yr >= Max_Rand) D_tube_yr = 0;
}
frames++;
/* handle keys */
if (kb_mode() && (kb_key(KB_SCAN_ESC) || kb_key(KB_SCAN_Q)))
return frames;
while (kb_kbhit())
{
int Factor;
unsigned key;
Factor = dz >= 10 ? 2 : 1;
key = kb_getkey();
switch(key)
{
case 0x1b: /* Esc */
case 'q':
case 'Q':
return frames;
case '+':
if (dz+Factor <= Max_Dz) dz += Factor;
break;
case '-':
if (dz-Factor >= Min_Dz) dz -= Factor;
break;
#if defined(USE_MIKMOD)
/* a little MOD-player :-) */
case 0x24b: /* cursor left */
if (in_sound && mod && !MP_Ready())
if (mp_sngpos > 0)
MP_PrevPosition();
break;
case 0x24d: /* cursor right */
if (in_sound && mod && !MP_Ready())
if (mp_sngpos < mod->numpos - 1)
MP_NextPosition();
break;
#endif
}
}
/* could wait for retrace here... */
/* waitvrt();